package servlet;

import java.io.*;
import java.sql.*;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import util.*;
import bll.*;
import model.*;
import dal.*;

/**
 * Servlet implementation class CompanyServlet
 */
@WebServlet(urlPatterns = { "/Phase", "/phase", "/PhaseServlet", "/phaseServlet" }, initParams = {
        @WebInitParam(name = "mode", value = "") })
public class PhaseServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * Default constructor.
	 */
	public PhaseServlet() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html; charset=UTF-8");

		String mode = request.getParameter("mode");

		if (mode.equalsIgnoreCase("new")) {

			if (RightUtil.<PhaseModel>checkInsertRight()) {

				// TODO : MORE CODE HERE

                getServletContext().getRequestDispatcher("/phaseNew.jsp").forward(request, response);
			} else {
				// TODO no right
			}
		} else if (mode.equalsIgnoreCase("edit")) {

                PhaseModel query = new PhaseModel();				
                String id = request.getParameter("id");
			query.setId(Integer.parseInt(id));


				GenericDal<PhaseModel, PhaseModelExt> dal = new GenericDal<PhaseModel, PhaseModelExt>(
						new PhaseModel());

				PhaseModel obj = null;
				try {
					obj = dal.select(query);
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (ClassNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				if (obj != null) {

					// TODO : MORE CODE HERE

                    request.setAttribute("phase", obj);
                    getServletContext().getRequestDispatcher("/phaseEdit.jsp").forward(request, response);

				} else {
					// TODO : data not found
				}			
		} else if (mode.equalsIgnoreCase("view")) {
			
                PhaseModel query = new PhaseModel();				
                String id = request.getParameter("id");
			query.setId(Integer.parseInt(id));


				GenericDal<PhaseModel, PhaseModelExt> dal = new GenericDal<PhaseModel, PhaseModelExt>(
						new PhaseModel());

				PhaseModel obj = null;
				try {
					obj = dal.select(query);
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (ClassNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				if (obj != null) {
					// TODO : LOAD MASTER DATA HERE
					// It's specific code that cannot be generated

                    request.setAttribute("phase", obj);
                    getServletContext().getRequestDispatcher("/phaseView.jsp").forward(request, response);

				} else {
					// TODO : data not found
				}			
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html; charset=UTF-8");

		String mode = request.getParameter("mode");

		if (mode.equalsIgnoreCase("insert")) {
			
				PhaseModel obj = new PhaseModel();

                String name = request.getParameter("name");
					obj.setName(name);

String code = request.getParameter("code");
					obj.setCode(code);

String description = request.getParameter("description");
					obj.setDescription(description);

String id_project = request.getParameter("id_project");
                    if (id_project != null && !id_project.isEmpty()) {
					    obj.setIdProject(Integer.parseInt(id_project));
                    }

String status = request.getParameter("status");
                    if (status != null && !status.isEmpty()) {
					    obj.setStatus(Integer.parseInt(status));
                    }


    String plan_start_date_date = request.getParameter("plan_start_date_date");
    String plan_start_date_time = request.getParameter("plan_start_date_time");
     if (plan_start_date_date != null && !plan_start_date_date.isEmpty())
     {
		if (plan_start_date_time != null && !plan_start_date_time.isEmpty()) 
		{
			obj.setPlanStartDate(DateTimeUtil.parseDateTime(plan_start_date_date,	plan_start_date_time));
		} 
		else 
		{
			obj.setPlanStartDate(DateTimeUtil.parseDate(plan_start_date_date));
		}
	}


    String plan_end_date_date = request.getParameter("plan_end_date_date");
    String plan_end_date_time = request.getParameter("plan_end_date_time");
     if (plan_end_date_date != null && !plan_end_date_date.isEmpty())
     {
		if (plan_end_date_time != null && !plan_end_date_time.isEmpty()) 
		{
			obj.setPlanEndDate(DateTimeUtil.parseDateTime(plan_end_date_date,	plan_end_date_time));
		} 
		else 
		{
			obj.setPlanEndDate(DateTimeUtil.parseDate(plan_end_date_date));
		}
	}


    String real_start_date_date = request.getParameter("real_start_date_date");
    String real_start_date_time = request.getParameter("real_start_date_time");
     if (real_start_date_date != null && !real_start_date_date.isEmpty())
     {
		if (real_start_date_time != null && !real_start_date_time.isEmpty()) 
		{
			obj.setRealStartDate(DateTimeUtil.parseDateTime(real_start_date_date,	real_start_date_time));
		} 
		else 
		{
			obj.setRealStartDate(DateTimeUtil.parseDate(real_start_date_date));
		}
	}


    String real_end_date_date = request.getParameter("real_end_date_date");
    String real_end_date_time = request.getParameter("real_end_date_time");
     if (real_end_date_date != null && !real_end_date_date.isEmpty())
     {
		if (real_end_date_time != null && !real_end_date_time.isEmpty()) 
		{
			obj.setRealEndDate(DateTimeUtil.parseDateTime(real_end_date_date,	real_end_date_time));
		} 
		else 
		{
			obj.setRealEndDate(DateTimeUtil.parseDate(real_end_date_date));
		}
	}



				

                obj.setCreator(SessionUtil.getCurrentUserID());

				if (CommonBll.checkInputs(obj)) {
					GenericDal<PhaseModel, PhaseModelExt> dal = new GenericDal<PhaseModel, PhaseModelExt>(
							new PhaseModel());

					int res = 0;
					try {
						res = dal.insert(obj);
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (ClassNotFoundException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

					if (res > 0) {
						// TODO : success, redirect to somewhere
					} else {
						// TODO : processing error here
					}
				} else {

					// invalid input

                    request.setAttribute("phase", obj);
                    getServletContext().getRequestDispatcher("/phaseInput.jsp").forward(request, response);

				}		
		} else if (mode.equalsIgnoreCase("update")) {		

                PhaseModel query = new PhaseModel();				
                String id = request.getParameter("id");
			query.setId(Integer.parseInt(id));


				GenericDal<PhaseModel, PhaseModelExt> dal = new GenericDal<PhaseModel, PhaseModelExt>(
						new PhaseModel());

				PhaseModel obj = null;
				try {
					obj = dal.select(query);
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (ClassNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				if (obj != null) {

                    String name = request.getParameter("name");
					obj.setName(name);

String code = request.getParameter("code");
					obj.setCode(code);

String description = request.getParameter("description");
					obj.setDescription(description);


					String id_project = request.getParameter("id_project");
					obj.setIdProject(Integer.parseInt(id_project));


					String status = request.getParameter("status");
					obj.setStatus(Integer.parseInt(status));

String plan_start_date_date = request.getParameter("plan_start_date_date");
					String plan_start_date_time = request.getParameter("plan_start_date_time");
        obj.setPlanStartDate(DateTimeUtil.parseDateTime(plan_start_date_date, plan_start_date_time));

String plan_end_date_date = request.getParameter("plan_end_date_date");
					String plan_end_date_time = request.getParameter("plan_end_date_time");
        obj.setPlanEndDate(DateTimeUtil.parseDateTime(plan_end_date_date, plan_end_date_time));

String real_start_date_date = request.getParameter("real_start_date_date");
					String real_start_date_time = request.getParameter("real_start_date_time");
        obj.setRealStartDate(DateTimeUtil.parseDateTime(real_start_date_date, real_start_date_time));

String real_end_date_date = request.getParameter("real_end_date_date");
					String real_end_date_time = request.getParameter("real_end_date_time");
        obj.setRealEndDate(DateTimeUtil.parseDateTime(real_end_date_date, real_end_date_time));



				

                    if (CommonBll.checkInputs(obj)) {	

					    int res = Integer.MIN_VALUE;
					    try {
						    res = dal.update(obj);
					    } catch (SQLException e) {
						    // TODO Auto-generated catch block
						    e.printStackTrace();
					    } catch (ClassNotFoundException e) {
						    // TODO Auto-generated catch block
						    e.printStackTrace();
					    }

					    if (res == 0) {
						    // TODO success
					    } else {
						    // TODO error
					    }
                    } else {
					request.setAttribute("phase", obj);
					getServletContext().getRequestDispatcher("/phaseEdit.jsp").forward(request, response);
				    }
				} else {
					// TODO data not found
				}			
		} else if (mode.equalsIgnoreCase("delete")) {		

                PhaseModel query = new PhaseModel();				
                String id = request.getParameter("id");
			query.setId(Integer.parseInt(id));


				GenericDal<PhaseModel, PhaseModelExt> dal = new GenericDal<PhaseModel, PhaseModelExt>(
						new PhaseModel());

				int res = Integer.MIN_VALUE;
				try {
					res = dal.delete(query);
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (ClassNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				if (res == 0) {
					// TODO success
				} else {
					// TODO fail
				}			
		}
	}

}